package by.yakusekmaxim.bookgid.webapp.page.home.author;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.dataaccess.util.Filter;
import by.yakusekmaxim.bookgid.datamodel.Author;
import by.yakusekmaxim.bookgid.datamodel.Author_;
import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.Book_;
import by.yakusekmaxim.bookgid.services.AuthorService;
import by.yakusekmaxim.bookgid.services.BookService;
import by.yakusekmaxim.bookgid.webapp.page.home.book.BookPage;

public class AuthorListPanel extends Panel{

	@Inject
	private AuthorService authorService;
	
	@Inject
	private BookService bookService;
	
	public AuthorListPanel(String id,final Filter<Author> filter) {
		super(id);
		
		AuthorDataProvider authorDataProvider = new AuthorDataProvider(
				filter);
		
		DataView<Author> dataView = new DataView<Author>("list",
				authorDataProvider, 10) {
			@Override
			protected void populateItem(Item<Author> item) {
				final Author author = item.getModelObject();
				
				Label imageLabel = new Label("image", "");
				imageLabel.add(AttributeModifier.replace("src", author.getImage()
						.getUrl()));
				imageLabel.add(AttributeModifier.replace("alt", author.getImage()
						.getName()));
				imageLabel.add(AttributeModifier.replace("title", author
						.getImage().getName()));
				item.add(imageLabel);
				
				Link<Object> authorDetailsPageLink = new Link<Object>("authorDetailsPageLink") {
					@Override
					public void onClick() {
						setResponsePage(new AuthorDetailsPage(author));
					}
				};
				
				authorDetailsPageLink.add(new Label("name"));
				item.add(authorDetailsPageLink);
				item.add(new Label("nameOr"));
				item.add(new Label("yearBirth",getDataFormat(author.getYearBirth())));
				Link<Object> linkAuthor = new Link<Object>("linkBook") {
					@Override
					public void onClick() {
						Filter<Book> filter = new Filter<Book>();
						filter.setEnabled(true);
						filter.setAttribute(Book_.author);
						filter.setValue(author);
						setResponsePage(new BookPage(filter, new ResourceModel(
								"p.admin.author.bookLink").getObject()
								+ author.getName()));

					}
				};
				linkAuthor.setEnabled(bookService
						.getCount(Book_.author, author) != 0);
				linkAuthor.add(new Label("bookCount", bookService.getCount(
						Book_.author, author)));
				item.add(linkAuthor);
				
			}

		};
		
		add(dataView);
		

		add(new PagingNavigator("paging", dataView));


		add(new OrderByBorder<SingularAttribute<Author, ?>>(
				"sortByName", Author_.name,
				authorDataProvider));
		add(new OrderByBorder<SingularAttribute<Author, ?>>(
				"sortByBirthplace", Author_.birthplace, authorDataProvider));
	}

	private class AuthorDataProvider
			extends
			SortableDataProvider<Author, SingularAttribute<Author, ?>> {

		private Filter<Author> filter;

		public AuthorDataProvider(Filter<Author> filter) {
			super();
			setSort(Author_.name, SortOrder.ASCENDING);
			this.filter = filter;

		}

		@Override
		public Iterator<? extends Author> iterator(long first, long count) {

			SingularAttribute<Author, ?> sortParam = getSort()
					.getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(
					sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);

			if (filter.getEnabled()) {
				return authorService.getAllAuthor(sortParam,
						ascending, (int) first, (int) count, filter).iterator();
			}
			return authorService.getAllAuthor(sortParam, ascending,
					(int) first, (int) count).iterator();
		}

		@Override
		public long size() {
			if (filter.getEnabled()) {
				return authorService.getCount(filter.getAttribute(),
						filter.getValue());
			}
			return authorService.getCount();
		}

		@Override
		public IModel<Author> model(Author author) {
			return new CompoundPropertyModel<Author>(author);
		}
	}
	
	private String getDataFormat(Date date){
		if(date==null){
			return "-";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
		return dateFormat.format(date);
	}
}
